<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Index extends Controller
{
//    首页结构主体
    public function index()
    {
        if (!empty($_SESSION['u_name'])) {
            return $this->fetch('index/index');
        }else{
            echo "<script>alert('您还没有登录！');top.location.href='/admin/login/login';</script>";
        }
    }

    //    首页主体内容页面
    public function main()
    {
        $startTime = date('Y-m-01', time());
        $endTime = date('Y-m-d', strtotime("$startTime +1 month"));
        $res = $this->search_time($startTime,$endTime);
        $this->assign('res',$res);

        if($_SESSION["u_r_id"]==1||$_SESSION["u_r_id"]==3||$_SESSION["u_r_id"]==2||$_SESSION["u_r_id"]==5||$_SESSION["u_r_id"]==6||$_SESSION["u_r_id"]==8||$_SESSION["u_r_id"]==9||$_SESSION["u_r_id"]==10||$_SESSION["u_r_id"]==12||$_SESSION["u_r_id"]==14){
            return $this->fetch('index/main');
        }else{
            return $this->fetch('index/main2');
        }
    }

    //    首页主体头部页面
    public function head()
    {
        return $this->fetch('index/head');
    }

    //    首页主体左边页面
    public function left()
    {
        return $this->fetch('index/left');
    }
    //   首页筛选功能
    public function search(Request $req){
        $startTime = $req->post('start');
        $endTime = $req->post('end');
        $endTime = date('Y-m-d', strtotime("$endTime +1 day"));
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        $res = $this->search_time($startTime,$endTime);
        echo json_encode($res);
    }
//    筛选功能
    private function search_time($startTime,$endTime){
        //当月上门量
        $res['sm'] = Db::table('mdm_job')
            ->field('count(*) as num')
            ->whereTime('job_get_time', 'between', [$startTime, $endTime])
            ->find();
//        var_dump(Db::table('mdm_job')->getLastSql());
        // 当月接待人数
        $res['jd'] = Db::table('mdm_job')
            ->field('count(*) as num')
            ->whereTime('job_inter_time', 'between', [$startTime, $endTime])
            ->find();
        // 当月试岗人数
        $res['sg'] = Db::table('mdm_job')
            ->field('count(*) as num')
            ->whereTime('job_try_time', 'between', [$startTime, $endTime])
            ->find();
        // 当月签单人数
        $res['qd'] = Db::table('mdm_job')
            ->field('count(*) as num')
            ->whereTime('job_order_time', 'between', [$startTime, $endTime])
            ->find();
        //当月求职中人数
        $res['qz'] = Db::table('mdm_job')
            ->field('count(*) as num')
            ->whereTime('job_graduate_time', 'between', [$startTime, $endTime])
            ->where('job_getjob_sn',2)
            ->find();
        //当月自主就业人数
        $res['jy'] = Db::table('mdm_job')
            ->field('count(*) as num')
            ->whereTime('job_getjob_time', 'between', [$startTime, $endTime])
            ->where('job_getjob_sn',4)
            ->find();
        return $res;
    }
    //    修改密码页面
    public function changepass(){
        if(!empty($_POST)){
            $name=$_SESSION['u_name'];
            $old=sha1(md5($_POST['old'])."mingdaima");
//            查看是否与旧密码匹配
            $re=db("user")->field("user_password")->where("user_name='$name'")->find();
            if($re['user_password']==$old){
                $new=sha1(md5($_POST['new'])."mingdaima");
                $renew=sha1(md5($_POST['renew'])."mingdaima");
                if($new==$renew){
                    $result=db("user")->where("user_name='$name'")->update(['user_password'=>$new]);
                    if($result){
                        echo 111;
                        echo "<script>alert('修改成功！');top.location.href='/admin/login/login';</script>";

                    }
                    else{
                        echo "<script>alert('修改失败！');</script>";
                    }
                }
                else{
                    echo "<script>alert('确认密码不匹配！');</script>";
                }
            }else{
                echo "<script>alert('密码输入错误！');</script>";
            }
        }
        return $this->fetch("password");
    }
}
